/**
 * 
 */
package com.designpattern.design.adapter;

/**
 * 适配器-类适配器
 * @author zhangyang
 *
 */
public class ClassAdapterTest {
	
	public static void main(String[] args) {
		/**
		 * 类适配器模式
		 */
		Phone phone = new Phone();
		//新建一个5V适配器
		VoltageClassAdapter adapter = new VoltageClassAdapter();
		phone.charge(adapter);
		/**
		 * 对象适配器模式
		 */
		VoltageObjectAdapter objectAdapter=new VoltageObjectAdapter(new Voltage220V());
		phone.charge(objectAdapter);
		
		/**
		 * 接口适配器
		 */
		VoltageInterfaceAdapter interfaceAdapter = new VoltageInterfaceAdapter(new Voltage220V());
		phone.charge(interfaceAdapter);
	}
}

/**
 * 原始类-220V
 * @author zhangyang
 *
 */
class Voltage220V{
	int src=220;
	
	/**
	 * 输出原始电压220V
	 * @return
	 */
	public int outPut220V() {
		return src;
	}
}

/**
 * 目标电压接口-转成5V的接口
 * @author zhangyang
 *
 */
interface Voltage5V{
	public int outPut5V();
}

/**
 * 电压转换接口-里面不仅有转5V 还能转10V 22V
 * @author zhangyang
 *
 */
interface VoltageChange extends Voltage5V{
	public int outPut10V();
	public int outPut22V();
}

/**
 * 电压类适配器-将220V电压转成5V
 * @author zhangyang
 *
 */
class VoltageClassAdapter extends Voltage220V implements Voltage5V{

	@Override
	public int outPut5V() {
		//首先获取元原对象的类属性
		int res=0;
		int outPut220V = this.outPut220V();
		//将原对象按照逻辑转成需要的对象
		res=outPut220V/44;
		return res;
	}
	
}

/**
 * 对象适配器-对象适配器是类适配器的改进，也常用
 * 有继承这个类变为持有这个类的实例作为改进，解决了继承的耦合问题
 * @author zhangyang
 *
 */
class VoltageObjectAdapter implements Voltage5V{
	Voltage220V voltage220V;
	
	
	public VoltageObjectAdapter(Voltage220V voltage220v) {
		super();
		voltage220V = voltage220v;
	}


	@Override
	public int outPut5V() {
		//首先获取元原对象的类属性
		int res=0;
		System.out.println("使用对象适配器转换");
		int outPut220V = voltage220V.outPut220V();
		//将原对象按照逻辑转成需要的对象
		res=outPut220V/44;
		return res;
	}
}


/**
 * 接口适配器-抽象类适配多个接口
 */
abstract class AbsVoltageInterfaceAdapter implements VoltageChange{
	Voltage220V voltage220V;


	public AbsVoltageInterfaceAdapter(Voltage220V voltage220v) {
		super();
		voltage220V = voltage220v;
	}

	/**
	 * 抽象类实现这个接口，将按个方法都实现了，单将需要实现的方法做空实现，方便子类重写
	 */
	@Override
	public int outPut5V() {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public int outPut10V() {
		// TODO Auto-generated method stub
		int res=0;
		System.out.println("使用了接口适配器来做适配");
		int outPut220V = this.voltage220V.outPut220V();
		res=outPut220V/22;
		return res;
	}

	@Override
	public int outPut22V() {
		// TODO Auto-generated method stub
		int res=0;
		System.out.println("使用了接口适配器来做适配");
		int outPut220V = this.voltage220V.outPut220V();
		res=outPut220V/10;
		return res;
	}
	
}

/**
 * 接口适配器-实现类 通过重写部分适配器方法来实现适配
 * @author zhangyang
 *
 */
class VoltageInterfaceAdapter extends AbsVoltageInterfaceAdapter{

	/**
	 * @param voltage220v
	 */
	public VoltageInterfaceAdapter(Voltage220V voltage220v) {
		super(voltage220v);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 重写抽象类的逻辑来实现适配器
	 */
	@Override
	public int outPut5V() {
		// TODO Auto-generated method stub
		int res=0;
		System.out.println("使用了接口适配器来做适配");
		int outPut220V = this.voltage220V.outPut220V();
		res=outPut220V/44;
		return res;
	}
	
}
/**
 * 手机,充电方法需要用到5V电压
 * @author zhangyang
 *
 */
class Phone{
	
	public void charge(Voltage5V voltage5V) {
		int outPut5V = voltage5V.outPut5V();
		if(outPut5V==5) {
			System.out.println("当前电压为5V，能够充电");
		}else if(outPut5V>5) {
			System.out.println("当前电压过高，不能充电");
		}else {
			System.out.println("当前电压过低，充电较慢");
		}
	}
}